﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for QuanTriVien
/// </summary>
public class QuanTriVienCtr
{
	public QuanTriVienCtr()
	{

	}

    public DataTable layQuanTri()
    {
        try
        {
            string sql = "select * from QuanTriVien ";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="quantri"></param>
    /// <returns>
    /// Quyen.QuanTri  1
    /// Quyen.NhanVien 2
    /// </returns>
    public Quyen dangNhap(EQuanTriVien quantri)
    {
        try
        {
            string sql = "select * from QuanTriVien WHERE  TenDN = @TenDN ";
            DataTable table = DataProvider.GetData(sql, "@TenDN", quantri.TenDN);
            if (table.Rows.Count > 0)
            {
                if(table.Rows[0]["quyen"].ToString().Equals("1"))
                    return Quyen.QuanTri;
                if (table.Rows[0]["quyen"].ToString().Equals("2"))
                    return Quyen.NhanVien;
            }
            return Quyen.Loi;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public EQuanTriVien layQuanTri(string TenDN)
    {
        try
        {
            DataTable tableQuanTri = DataProvider.GetData("Select * from QuanTriVien WHERE TenDN=@TenDN","@TenDN",TenDN);
            EQuanTriVien quanTri = new EQuanTriVien();
            quanTri.TenDN = tableQuanTri.Rows[0]["TenDN"].ToString();
            quanTri.MatKhau = tableQuanTri.Rows[0]["MatKhau"].ToString();
            quanTri.Quyen = tableQuanTri.Rows[0]["Quyen"].ToString();
            quanTri.LanDNCuoi = DateTime.Parse(tableQuanTri.Rows[0]["LanDNCuoi"].ToString());
            quanTri.TrangThai = tableQuanTri.Rows[0]["TrangThai"].ToString();
            return quanTri;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public bool InsertQuanTri(EQuanTriVien quanTri)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO QuanTriVien VAlUES(@TenDN,@MatKhau,@Quyen,@LanDNCuoi,@TrangThai)",
                 "@TenDN", quanTri.TenDN, "@MatKhau", quanTri.MatKhau, "@LanDNCuoi", quanTri.LanDNCuoi, "@Quyen", quanTri.Quyen, "@TrangThai", quanTri.TrangThai);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }

    public bool DeleteQuanTri(string TenDN)
    {
        try
        {
            DataProvider.ExecuteNonQuery("DELETE FROM QuanTriVien WHERE TenDN=@TenDN", "@TenDN", TenDN);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }

    public bool UpdateQuanTri(EQuanTriVien quanTri)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update QuanTriVien set MatKhau=@MatKhau,Quyen=@Quyen,LanDNCuoi=@LanDNCuoi,TrangThai=@TrangThai WHERE TenD=@TenDN", "@TenDN", quanTri.TenDN, "@MatKhau", quanTri.MatKhau, "@Quyen", quanTri.Quyen, "@LanDNCuoi", quanTri.LanDNCuoi, "@TrangThai", quanTri.TrangThai);
            return true;
        }
        catch (Exception ex)
        {
            //throw ex;
            return false;
        }
    }
    //Hanh
    public DataTable layThongTinThanhVienH()
    {
        try
        {
            string sql = "select * from QuanTriVien a,NhanVien b where a.TenDN=b.TaiKhoan;";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public DataTable layThongTinThanhVienH(string tenDN)
    {
        try
        {
            string sql = "select * from QuanTriVien a,NhanVien b where a.TenDN=b.TaiKhoan and a.TenDN=@tenDN";
            return DataProvider.GetData(sql, "@tenDN", tenDN);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public bool UpdateQuanTriH(string chucvu, string tendangnhap)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update QuanTriVien set Quyen=@Quyen WHERE TenDN=@TenDN", "@TenDN", tendangnhap, "@Quyen", chucvu);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable timkiemthanhvien(string chuoitk, int loaitk)
    {
        string sql = "DECLARE	@return_value int EXECUTE TimKiemThanhVien @ChuoiTimKiem = @a,@LoaiTK=@b";
        return DataProvider.GetData(sql, "@a", chuoitk, "@b", loaitk);
    }
    public Quyen dangNhapH(EQuanTriVien quantri)
    {
        try
        {
            string sql = "select * from QuanTriVien WHERE  TenDN = @TenDN and MatKhau=@MatKhau ";
            DataTable table = DataProvider.GetData(sql, "@TenDN", quantri.TenDN, "@MatKhau", quantri.MatKhau);
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["quyen"].ToString().Equals("QuanTri"))
                    return Quyen.QuanTri;
                if (table.Rows[0]["quyen"].ToString().Equals("NhanVien"))
                    return Quyen.NhanVien;
            }
            return Quyen.Loi;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}